package com.gxc.tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 树的宽度遍历
 */
public class WidthTraversal {

    public static void main(String[] args) {
        TreeBean tree = Preorder.createTree();

        traversal(tree);
    }

    /**
     * 树的宽度遍历--队列实现
     * @param tree
     */
    private static void traversal(TreeBean tree) {
        Queue<TreeBean> queue = new LinkedList<>();
        queue.add(tree);
        while (!queue.isEmpty()) {
            TreeBean treeBean = queue.poll();
            System.out.print(treeBean.value + "  ");
            if (treeBean.left!=null) queue.add(treeBean.left);
            if (treeBean.right!=null) queue.add(treeBean.right);
        }
    }
}
